/* 
 * FST Extension: Photojournal
 * Abe Yang <abeyang@cal.berkeley.edu> (c) 2007
 * http://code.google.com/p/flickrsetsbytag/
 *
/* ---------------------------------------------------------------------------------- */

var Journalentry = Class.create();

Journalentry.prototype = {
	initialize: function(external_id, size) {
		// common variables
		this.id			= external_id;
		this.index 		= 0;
		this.size		= size;
		this.isfirsttime = true;
		this.hasshownthumbs = false;
		this.photos		= {};
		
	},	// end initialize()
	
	showSlideshow: function() {
		$('linkhide' + this.id).show();
		$('linkshow' + this.id).hide();
		new Effect.SlideDown('slideshow' + this.id);
		this.getCurrent();
		
		// set 'isfirsttime' to false after hitting this fn for the first time
		if (this.isfirsttime) this.isfirsttime = false;
	},	// end showSlideshow()
	
	hideSlideshow: function(duration) {
		$('linkhide' + this.id).hide();
		$('linkshow' + this.id).show();
		new Effect.SlideUp('slideshow' + this.id, {duration:duration});
	},	// end hideSlideshow()
	
	getCurrent: function() {
		// if first time, don't do anything (fix for IE6)
		if (!this.isfirsttime) this.getSlide(this.index);
	},	// end getCurrent()
	
	getNext: function() {
		var i = ++this.index % this.size;
		this.getSlide(i);
	},	// end getNext()
	
	getPrev: function() {
		var i = this.index;
		if (--i < 0) i = this.size - 1;		// modulo doesn't work in this case..
		this.getSlide(i);
	},	// end getPrev()
	
	// Generic function that fetches a particular slideshow obj, given the index
	// This should be treated as a "private" method; should not be called from the outside
	getSlide: function(index) {
		this.index = index;		// update object index
		var link = buildPhotoUrl(this.photos[index]['id'], '');
		var caption = this.photos[index]['caption'];
		
		$('currentphoto' + this.id).innerHTML = index + 1;
		$('image' + this.id).innerHTML = '<img src="' + link + '" alt="" />';
		$('caption' + this.id).innerHTML = (caption == '') ? '<p>&nbsp;</p>' : '<p>' + caption + '</p>';
	},	// end getSlide()
	
	showThumbnails: function() {
		$('hidethumbs' + this.id).show();
		$('showthumbs' + this.id).hide();
		$('thumbgallery' + this.id).toggle();
		
		// construct thumbnails only upon hitting the fn for the first time
		if (!this.hasshownthumbs) {
			var textholder = '';
			for (var i=0; i<this.size; i++) {
				textholder += '<img src="' + buildPhotoUrl(this.photos[i]['id'], '_s') + '" onclick="album' + this.id + '.getSlide(' + i + ');" />';
			}
			
			$('longstrip' + this.id).update(textholder)
			this.hasshownthumbs = true;
		}
		
		$('track' + this.id).style.visibility = 'visible';
	},	// end showThumbnails()
	
	hideThumbnails: function() {
		$('hidethumbs' + this.id).hide();
		$('showthumbs' + this.id).show();
		$('thumbgallery' + this.id).toggle();
		
		$('track' + this.id).style.visibility = 'hidden';
	},	// end hideThumbnails()
	
	showLargeImage: function() {
		var link =  buildPhotoUrl(this.photos[this.index]['id'], '_b');
		window.open(link,'image'+this.id+this.index,'resizable=1, status=0, menubar=0, toolbar=0, scrollbars=1, location=0, directories=0');
	}	// end showLargeImage()
	
};	// end Journalentry.prototype

// toggle visibility (NOT display)
function ToggleViz (node) {
        node = $(node);
        node.style.visibility = (node.style.visibility == "hidden") ? "visible" : "hidden";
}

// Function that constructs a photo's Flickr URL
function buildPhotoUrl (photoid, size) {
	var photo_url = "http://static.flickr.com/";
	return photo_url + photoid + size + '.jpg';
}

// function maps thumbnail scroller to thumbnail "longstrip"
function scrollToPos(id, v) {
     $(id).style.left = (-v) + 'px';
}
